/**
 * Company : Thai Informatic System Co., Ltd.
 * Created By : Dr.Tree
 * Created On : Feb 13, 2012  10:02:09 PM 
 * e-Mail : chatree@tis.co.th
 * Mobile Phone : (+66) 087-921-5115
 * Package Name : jpa.service.promis.common
 * File Name : UmMasterServices.java
 * TODO
 */
package jpa.service.promis.common;

import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import org.apache.log4j.Logger;
import promis.utility.CommonService;
import jpa.entitys.common.CommonEntity;
import jpa.entitys.promis.common.UmMaster;
import jpa.service.common.JPAService;

/**
 * @author chatree
 *
 */
public class UmMasterServices extends JPAService {
	
	private static final Logger log = Logger.getLogger(UmMasterServices.class);

	public UmMasterServices(EntityManager em) {
		super(em);
		// TODO Auto-generated constructor stub
	}
	
	public UmMaster findById(String id) throws Exception {
		
		return super.ConvertToYoureObject(super.find(id, "UmMaster.findById"));				
	}
	
	
	public UmMaster InsertedOrUpdated(UmMaster vlaue) throws Exception {
		UmMaster obj = vlaue;
		try{		
			if(CommonService.IsNull(obj.getLastUpdOn()))
			{			
				obj.setSyncCreatedby(this.getUser());
				obj.setSyncCreatedon(this.getCurrent_date());
				obj.setSyncUpdatedby(this.getUser());
				obj.setSyncUpdatedon(this.getCurrent_date());
				super.inserted(obj);
			}else{			
				obj.setSyncUpdatedby(this.getUser());
				obj.setSyncUpdatedon(this.getCurrent_date());
				super.updated(obj);
			}
		}catch(Exception e)
		{
			log.error(e);
			throw e;
		}
		return obj;
	}
	
	public List<UmMaster> Inserted(List<UmMaster> vlauelist) throws Exception {
		try{
			List<CommonEntity> common_lst = new ArrayList<CommonEntity>();			
			for(UmMaster obj : vlauelist)
			{
				if(CommonService.IsNull(obj.getLastUpdOn()))
				{			
					obj.setSyncCreatedby(this.getUser());
					obj.setSyncCreatedon(this.getCurrent_date());
					obj.setSyncUpdatedby(this.getUser());
					obj.setSyncUpdatedon(this.getCurrent_date());
				}
			}		
			
			if(CommonService.getSize(common_lst) > 0)
				super.inserted(common_lst);
			
		}catch(Exception e)
		{
			log.error(e);
			throw e;
		}
		return vlauelist;
	}
	
	public List<UmMaster> Updated(List<UmMaster> vlauelist) throws Exception {
		try{
			List<CommonEntity> common_lst = new ArrayList<CommonEntity>();			
			for(UmMaster obj : vlauelist)
			{
				if(CommonService.IsNull(obj.getLastUpdOn()))
				{
					obj.setSyncUpdatedby(this.getUser());
					obj.setSyncUpdatedon(this.getCurrent_date());
					
					common_lst.add(obj);					
				}
			}
			
			if(CommonService.getSize(common_lst) > 0)
				super.updated(common_lst);
			
		}catch(Exception e)
		{
			log.error(e);
			throw e;
		}
		return vlauelist;
	}
	
}
